<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Category model
 *
 * @subpackage	Category Module
 * @category	Modules
 * @author lethanhquang910@gmail.com
 */
class Category_m extends CI_Model {

    /**
     * Get all category by parent id
     * @param type $parentId
     * @return type 
     */
    public function get_all_by_parent_id($parentId) {
        return $this->db->where('parent_id', $parentId)
                        ->get('category')
                        ->result_array();
    }

    /**
     * Get all category
     * @param type $arrParams
     * @return type 
     */
    public function get_all($arrParams = NULL) {
        if ($arrParams) {
            $this->db->where($arrParams);
        }

        return $this->db->get('category')
                        ->result_array();
    }

    
    /**
     * Get category tree
     * @param array $arrParams
     */
    public function get_tree($arrParams = NULL)
    {
        $arrCategory = $this->get_all($arrParams);
        
        if (sizeof($arrCategory) > 0) {
            $arrNewCategory = array();
            foreach ($arrCategory as $cate) {
                if ($cate['parent_id'] == 0) {
                    $arrNewCategory[$cate['category_id']] = $cate;
                } else {
                    $arrNewCategory[$cate['parent_id']]['child'][$cate['category_id']] = $cate;
                }
            }

            $arrCategory = $arrNewCategory;
        }
        
        return $arrCategory;
    }

}